package pacman.entries.pacman.ml.features;

import pacman.game.Game;
import pacman.game.Constants.GHOST;

// returns a value between 0 and 1
// the feature = 2/(1 + Exp[-(# junctions)]) - 1
public class JunctionsToGhostFeature extends FeatureExtractor {
	private GHOST target;
	public JunctionsToGhostFeature(float ghost) {
		target = GHOST.values()[(int)ghost];
	}
	public float Extract(Game game) {
		int pacmanNode = game.getPacmanCurrentNodeIndex();
		int ghostNode = game.getGhostCurrentNodeIndex(target);
		int junctions = 0;

		int[] path = new int[]{};
		boolean exception = false;
		try {
			path = game.getShortestPath(pacmanNode, ghostNode);
		}
		catch (Exception e){
			exception = true;
		}
		if(!exception) {
			for(int node : path) {
				if(game.isJunction(node)) {
					junctions++;
				}
			}
		}

		return (float)(2.0/(1 + Math.exp(-junctions)) - 1);  // logistic function
	}
}